package com.example.test.springdemo.service.impl;

import com.example.test.springdemo.common.Result;
import com.example.test.springdemo.domain.AdminRole;
import com.example.test.springdemo.domain.AdminRoleExample;
import com.example.test.springdemo.mapper.AdminRoleMapper;
import com.example.test.springdemo.mapper.RoleListMapper;
import com.example.test.springdemo.service.AdminRoleListService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class AdminRoleListServiceImpl implements AdminRoleListService {
    @Autowired
    private AdminRoleMapper adminRoleMapper;

    @Autowired
    private RoleListMapper roleListMapper;

    @Override
    public Result addRole(AdminRole adminRole) {
        try{
            adminRole.setGmtCreateTime(new Date());
            int n = adminRoleMapper.insert(adminRole);
            if (n > 0){
                return Result.success("添加成功!");
            }else{
                return Result.failure("添加失败!");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器异常:"+e.getMessage());
        }
    }

    @Override
    public Result delRole(Long id) {
        adminRoleMapper.deleteByPrimaryKey(id);
        return Result.success("删除成功！");
    }

    @Override
    public void updateRole(AdminRole adminRole){
        adminRole.setGmtCreateTime(new Date());
//        AssertUtil.state(adminRoleMapper.updateByPrimaryKeySelective(adminRole)>0,"修改失败！");
    }

    @Override
    public Integer allResource(Long id, Long... ids) {
        Integer integer = roleListMapper.allResource(id, ids);
        return integer;
    }

    @Override
    public Integer allMenus(Long id, Long... ids) {
        Integer integer = roleListMapper.addMenus(id, ids);
        return integer;
    }

    @Override
    public PageInfo selectRolePage(Integer pageNow, Integer pageSize, String roleName) {
        PageHelper.startPage(pageNow,pageSize);
        List<AdminRole> adminRoles = roleListMapper.selectRolePage(roleName);
        PageInfo pageInfo = new PageInfo(adminRoles);
        return pageInfo;
    }

    @Override
    public List<AdminRole> roleInfo() {
        AdminRoleExample adminRoleExample = new AdminRoleExample();
        return adminRoleMapper.selectByExample(adminRoleExample);
    }

    @Override
    public List<AdminRole> findRoleList(AdminRole adminRole) {
        return adminRoleMapper.findRoleList(adminRole);
    }

    @Override
    public AdminRole findByRoleId(Long id) {
        return adminRoleMapper.selectByPrimaryKey(id);
    }
}
